package com.api.weavernorth.LogWeaTable.cmd;

import com.api.weavernorth.LogWeaTable.pojo.logdataPojo;
import com.api.weavernorth.LogWeaTable.service.Impl.LogDataServiceImpl;
import com.cloudstore.eccom.constant.WeaBoolAttr;
import com.cloudstore.eccom.pc.table.WeaTable;
import com.cloudstore.eccom.pc.table.WeaTableColumn;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import weaver.general.PageIdConst;
import weaver.general.Util;
import weaver.hrm.User;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: BruseeWang
 * @Date: 2021/12/08 13:23
 * @Description: 扫描服务器日志并组装成一个表格
 */
public class WeaTableLog extends AbstractCommonCommand<Map<String, Object>> {

    public WeaTableLog(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
    }


    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Logger log = Logger4jUtil.getLoggerByName("/api/weavernorth/LogWeaTable/");
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user) {
            apidatas.put("hasRight", false);
            return apidatas;
        }
        try {
            //返回消息结构体
            WeaResultMsg result = new WeaResultMsg(false);
            String pageID = "17ecc14c-9d64-4a43-81a4-7b7c6f792857";
            String pageUid = pageID + "_" + user.getUID();
            String pageSize = PageIdConst.getPageSize(pageID, user.getUID());
            String sqlwhere = " 1=1 ";
            log.info("params="+params.toString());
            LogDataServiceImpl service=new LogDataServiceImpl();
            List<logdataPojo> log_List = service.Operation_LogData(log,params,false);
            //新建一个weatable
            WeaTable table = new WeaTable();
            table.setPageUID(pageUid);
            table.setPageID(pageID);
            table.setPagesize(pageSize);

            String fileds = " id,logname,hosts,ljlx,logdate,logLink ";
            table.setBackfields(fileds);

            //搜索条件,这里可以放高级搜索的的条件
            String logname = Util.null2String(params.get("logname"));
            if (StringUtils.isNotBlank(logname)) {
                sqlwhere += " and l.logname like '%" + logname + "%' ";
            }

            //路径类型
            String ljlx = Util.null2String(params.get("ljlx"));
            if (StringUtils.isNotBlank(ljlx)) {
                sqlwhere += " and l.ljlx like '%" + ljlx + "%' ";
            }
            table.setSqlform(" uf_LogRecord_Table");
            log.info("日志查询语句："+sqlwhere);
            table.setSqlwhere(sqlwhere);
            table.setSqlorderby("id");
            table.setSqlprimarykey("id");
            table.setSqlisdistinct("false");
            table.getColumns().add(new WeaTableColumn("id").setDisplay(WeaBoolAttr.FALSE));   //设置为不显示
            table.getColumns().add(new WeaTableColumn("20%", "日志名称", "logname"));
            table.getColumns().add(new WeaTableColumn("20%", "服务器", "hosts"));
            table.getColumns().add(new WeaTableColumn("20%", "路径类型", "ljlx"));
            table.getColumns().add(new WeaTableColumn("20%", "日期", "logdate"));
            table.getColumns().add(new WeaTableColumn("20%", "日志", "logLink","logLink","com.api.weavernorth.LogWeaTable.cmd.WeaTableTransMethod.getHref"));

            //设置左侧check默认不存在
            table.setCheckboxList(null);
            table.setCheckboxpopedom(null);

            result.putAll(table.makeDataResult());
            result.put("hasRight", true);
            result.success();
            apidatas = result.getResultMap();
            log.info("查询后的结果集："+apidatas.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return apidatas;
    }



}
